/**
 * Script of the settings page
 */
var PageOptions = new (function() {
	
	var options;
	
	/**
	 * Load the settings
	 */
	function load() {
		MikuClip.loadOptions();
		options = MikuClip.options;
		$("#options-youtube-quality").val(options.youtubeQuality);
		$("#options-login").val(options.login);
		$("#options-password").val(options.password);
		$("#options-lang").val(options.lang);
		
		$("#options-lang").selectmenu("refresh");
		$("#options-youtube-quality").selectmenu("refresh");
	}
	
	/**
	 * Save the settings
	 * Refresh the page if lang is changed
	 */
	function save() {
		var newoptions = {
			login:			$("#options-login").val(),
			password:		$("#options-password").val(),
			youtubeQuality:	$("#options-youtube-quality").val(),
			lang: 			$("#options-lang").val()
		}
		var refreshPage = false;
		if (newoptions.lang != MikuClip.options.lang) {
			refreshPage = true;
		}
		MikuClip.setOptions(newoptions);
		
		if (refreshPage) {
			location.reload();
		}
	}
	
	
	$("body").on("pagebeforeshow", "#page-options", load);
	$("#page-options").on("change", "#options-youtube-quality", save);
	$("#page-options").on("change", "#options-lang", save);
	$("#page-options").on("keyup", "#options-login", save);
	$("#page-options").on("keyup", "#options-password", save);
	
});